#include "replaceby.h"
#include "ui_replaceby.h"

ReplaceBy::ReplaceBy (QWidget *parent) :
	QDialog (parent),
	ui (new Ui::ReplaceBy)
{
	ui->setupUi (this);
}

ReplaceBy::~ReplaceBy()
{
	delete ui;
}

int ReplaceBy::run (const QString &bad, const QStringList &goodList, QString* good)
{
	m_bad = bad;
	ui->good->setText (bad);
	setWindowTitle (tr ("Remplacer %1 par").arg (bad));
	ui->goodList->addItems (goodList);
	ui->here->setDisabled (true);
	ui->inAllText->setDisabled (true);
	ui->always->setDisabled (true);

	exec ();

	*good = ui->good->text ();
	return m_retValue;
}


void ReplaceBy::on_here_clicked()
{
	m_retValue = ReplaceHere;
	accept ();
}

void ReplaceBy::on_inWholeText_clicked()
{
	m_retValue = ReplaceEveryWhere;
	accept ();
}

void ReplaceBy::on_always_clicked()
{
	m_retValue = ReplaceAllways;
	accept ();
}

void ReplaceBy::on_cancel_clicked()
{
	m_retValue = Cancel;
	reject ();

}

void ReplaceBy::on_goodList_currentRowChanged(int currentRow)
{
	if (currentRow != -1) {
		ui->good->setText (ui->goodList->currentItem ()->text ());
	}
}

void ReplaceBy::on_good_textChanged(const QString &arg1)
{
	bool changed = (arg1 != m_bad && !arg1.isEmpty ());
	ui->here->setEnabled (changed);
	ui->inAllText->setEnabled (changed);
	ui->always->setEnabled (changed);
	ui->goodList->setCurrentRow (-1);
}
